package com.ibm.spatiotemp.test.requestServer;

import com.ibm.spatiotemp.datamodel.config.PubSubMeta;
import com.ibm.spatiotemp.test.requestServer.redis.RedisPubSub;

public class JedisPubSubListener extends RequestListener{

	public RedisPubSub pubsuber;
	public RedisPubSub getPubsuber() {
		return pubsuber;
	}


	public void setPubsuber(RedisPubSub pubsuber) {
		this.pubsuber = pubsuber;
	}


	public PubSubMeta getPubsubinfo() {
		return pubsubinfo;
	}


	public void setPubsubinfo(PubSubMeta pubsubinfo) {
		this.pubsubinfo = pubsubinfo;
	}


	public PubSubMeta pubsubinfo;
	/**
	 * for each pubsubmeta related to the geoindexdb server, create JedisPubSubListener
	 * @param pubsubmeta
	 */
	public JedisPubSubListener(PubSubMeta pubsubmeta){
		pubsubinfo = pubsubmeta;
		pubsuber = new RedisPubSub();
		pubsuber.init(pubsubmeta.getServerip(), ""+pubsubmeta.getPort());
		pubsuber.subscribe(pubsubmeta.getTopic());
	}
	

	@Override
	public String nextRequest() {
		while(true){
			String[] res = pubsuber.getMessage();
			if(res != null && res[1] != null){
				return res[1];
			}
		}
		
	}

}
